package com.basedefence;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.view.MotionEvent;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Drawable;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.event.SceneUpdateListener;

//import android.app.Activity;
//import android.os.Bundle;
//
//public class BaseDefenceActivity extends Activity {
//    /** Called when the activity is first created. */
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//    }
//}

public class BaseDefenceActivity extends E3Activity implements SceneUpdateListener
{
//	AnimatedSprite rocket;
	TiledTexture rocketTexture;
	public static int m_nScrW;
	public static int m_nScrH;
	
	ArrayList<Rocket> rockets;
	Base baseObject;
	
	@Override
	public E3Engine onLoadEngine() {
		m_nScrH = getDisplayMetrics().heightPixels;
		m_nScrW = getDisplayMetrics().widthPixels;
		E3Engine engine = new E3Engine(this, m_nScrH, m_nScrW);
		engine.requestFullScreen();
		engine.requestPortrait();
		E3Activity.setCurrentActivity(this);
		return engine;
	}

	@Override
	public void onLoadResources() {
//		rocketTexture = new TiledTexture("rocket.png", 201, 272, this);
		if (SpriteLoader.getAnimation("rocket.png", 0) == null)
			SpriteLoader.loadSprite("rocket.png");
		if (SpriteLoader.getAnimation("base.png", 0) == null)
			SpriteLoader.loadSprite("base.png");
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.addEventListener(this);
		scene.registerUpdateListener(1000 / 60, this);
		
		rockets = new ArrayList<Rocket>();
		
		AddBase(scene);
		
		AddRocketPair(scene);
				
		scene.setBackgroundColor(0, 0, 0, 1);	
		
		return scene;
	}
	
	private void AddRocketPair(E3Scene scene)
	{
//		AddNewRocket(scene, 50, 50, 100, m_nScrW / 2, 400);
//		AddNewRocket(scene, m_nScrW - 50, 50, 100, m_nScrW / 2, 400);
		AddNewRocket(scene, 50, 50, 100, baseObject.getCenterX(), baseObject.getCenterY());
		AddNewRocket(scene, m_nScrW - 50, 50, 100, baseObject.getCenterX(), baseObject.getCenterY());
	}

	private void AddBase (E3Scene scene) 
	{
		AnimatedSprite baseSpr = SpriteLoader.getAnimation("base.png", 0);
		
		int cX = m_nScrW / 2;
		int cY = m_nScrH - 10;
		
		baseSpr.move((int)(cX - baseSpr.getWidthScaled() / 2), (int)(cY - baseSpr.getHeightScaled()));
		baseSpr.scale(0.45f, 0.35f);
		
		baseObject = new Base();
		baseObject.setSprite(baseSpr);
		baseObject.setCenter(cX, cY);
		
		scene.getTopLayer().add(baseSpr);
	}

	private void AddNewRocket (E3Scene scene, int x, int y, int speed, int targetX, int targetY)
	{
		AnimatedSprite rocketSprite = SpriteLoader.getAnimation("rocket.png", 0);
		int rCenterX = (int) (rocketSprite.getWidthScaled()  / 2);
		int rCenterY = (int) (rocketSprite.getHeightScaled() / 2);

		rocketSprite.move(x - rCenterX, y - rCenterY);
		rocketSprite.setRotationCenter(rCenterX, rCenterY);
		rocketSprite.scale(0.5f, 0.5f);
		
		Rocket rocket = new Rocket();
		rocket.setSprite(rocketSprite);
		rocket.setSpeed(speed);
		
		Vector2 v = new Vector2(targetX - rocketSprite.getRealX(), targetY - rocketSprite.getRealY());
		rocket.setDirection(v.nor(), true);
		
		rockets.add(rocket);
		
		scene.getTopLayer().add(rocketSprite);
	}

	public void onUpdateScene(E3Scene scene, long elapsedTime) {
		if (Debug.useStaticTime)
			elapsedTime = 30;
				
		for (int i = rockets.size() - 1; i > 0; i--)
		{
			Rocket currRocket = rockets.get(i);
			if (currRocket.getState() == RocketState.died)
				RemoveRocket(scene, currRocket);
		}
		
		for (Rocket currRocket : rockets) {
			currRocket.update(elapsedTime);
			
			for (Rocket rocketToCheck : rockets) {
				if (rocketToCheck.getState() == RocketState.idle && currRocket.getState() == RocketState.idle)
				{
					if (rocketToCheck != currRocket && rocketToCheck.getSprite().collidesWith(currRocket.getSprite()))
					{
						rocketToCheck.destroy();
						currRocket.destroy();
						AddRocketPair(scene);
						return;
					}
				}
			}
		}
	}
	
	private void RemoveRocket(E3Scene scene, Rocket currRocket) {
		rockets.remove(currRocket);
		currRocket.getSprite().hide();
		scene.getTopLayer().remove(currRocket.getSprite());
	}

	public void onStop() 
	{
		super.onStop();
		SpriteLoader.unloadSprite("rocket.png");
		TextureManager.unloadTexture("rocket.png");
		SpriteLoader.unloadSprite("base.png");
		TextureManager.unloadTexture("base.png");
	}
	
	public boolean onSceneTouchEvent (final E3Scene scene, final MotionEvent motionEvent) 
	{
		if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) 
		{
			int x = getTouchEventX(scene, motionEvent);
			int y = getTouchEventY(scene, motionEvent);
			
			Drawable obj;
			if ((obj = scene.findDrawableAt(x, y)) != null) 
			{
				AnimatedSprite spr = (AnimatedSprite)obj;
				Rocket currRocket = getRocket(spr);
				if (currRocket != null && !currRocket.wasTapped)
				{
					if (currRocket.getDirectionX() < 0)
						currRocket.addAngle(20);
					else
						currRocket.addAngle(-20);
					currRocket.wasTapped = true;
				}
			}
		}
		
		return false;
	}
	
	public Rocket getRocket (AnimatedSprite sprite)
	{
		for (Rocket currRocket : rockets)
		{
			if (currRocket.getSprite().equals(sprite))
				return currRocket;
		}
		
		return null;
	}

}